package com.example.blood_sugar.fragments;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.blood_sugar.R;
import com.example.blood_sugar.bean.BSRecord;
import com.example.blood_sugar.db.BSRecordHolder;
import com.example.blood_sugar.utils.DateUtil;
import com.example.blood_sugar.view.LineGraphicView;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 展示血压的曲线图Fragment
 */
public class BloodPressureChartFragment extends BaseFragment {

    private ArrayList<Double> yList1; // 收缩压y坐标
    private ArrayList<Double> yList2; // 舒张压y坐标

    private ArrayList<String> xRawDatas; // 横轴标示数据 （日期）
    private LineGraphicView mLineGraphicView1; // 收缩压表格
    private LineGraphicView mLineGraphicView2; // 舒张压表格

    private List<BSRecord> mBSRecords;

    private int count = 30; // 只显示30天的记录

    /**
     * 构建本类实例
     * @param text
     * @return
     */
    public static BloodPressureChartFragment newInstance(String text){
        Bundle bundle = new Bundle();
        bundle.putString("text",text);
        BloodPressureChartFragment bloodPressureChartFragment = new BloodPressureChartFragment();
        bloodPressureChartFragment.setArguments(bundle);
        return  bloodPressureChartFragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_blood_pressure_chart, container, false);
        mLineGraphicView1 = view.findViewById(R.id.line_graphic1);
        mLineGraphicView2 = view.findViewById(R.id.line_graphic2);

        yList1 = new ArrayList<>();
        yList2 = new ArrayList<>();

        xRawDatas = new ArrayList<>();
        for (int i = 0; i < count; i ++) {
            xRawDatas.add(DateUtil.date2Str(DateUtil.getPreDate(new Date(), count - 1 - i), "MM月dd日"));
        }

        initView();

        return view;
    }

    /**
     * 初始化界面 （更新界面）
     */
    public void initView(){
        if (yList1 == null)
            return;
        mBSRecords = BSRecordHolder.get(getContext()).getBSRecords(count);
        if (yList1.size() > 0){
            yList1.clear();
            yList2.clear();
        }
        for (int i = 0; i < count; i ++){
            BSRecord bsRecord = mBSRecords.get(i);
            yList1.add((double) bsRecord.getSystolicBP());
            yList2.add((double) bsRecord.getDiastolicBP());
        }

        mLineGraphicView1.setData(yList1, xRawDatas, 180, 30);
        mLineGraphicView2.setData(yList2, xRawDatas, 180, 30);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        // TODO: 2018/8/21 创建成功后初始化操作？
    }

    /**
     * 恢复时更新界面
     */
    @Override
    public void onResume() {
        super.onResume();
        initView();
    }

    /**
     * UserVisible改变时更新界面
     * @param isVisibleToUser
     */
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        initView();
    }
}
